# -*- coding: utf-8 -*-
"""
Created on Fri Oct 12 15:21:10 2018

@author: caixue1
"""

import sys
sys.path.append("D:\Project\Argo")
sys.path.append("D:\Program Files\Python36\Lib\site-packages")
import datetime
import os
import pandas as pd
from sklearn.utils import shuffle
from sklearn import tree
#from sklearn.decomposition import PCA
from sklearn import preprocessing
import numpy as np
from sklearn.metrics import accuracy_score, auc, confusion_matrix, f1_score, precision_score, recall_score,roc_curve  # 导入指标库
import prettytable  # 导入表格库
import pydotplus  # 导入dot插件库
import matplotlib.pyplot as plt # 导入图形展示库
import copy
import pymongo
import Core.Gadget as Gadget


# 根据决策树选股统计收益


path = 'D:/StrategyData'
filename = os.listdir(path)
sel = filename[88:]
for file in sel:
    data = pd.read_csv('D:/StrategyData/' + file, encoding='GBK').set_index('Unnamed: 0')



datetime1 = datetime.datetime(2010,7,30)
datetime2 = datetime.datetime(2018,9,1)
datetime1 = Gadget.ToUTCDateTime(datetime1)
datetime2 = Gadget.ToUTCDateTime(datetime2)
datetimes = Gadget.GenerateEndDayofMonth(datetime1, datetime2)

dt = datetimes[91]
referenceDate = dt + datetime.timedelta(days=-210)
recentMonths = Gadget.GenerateEndDayofMonth(referenceDate, dt)[0:6]

backtest = []
for file in recentMonths:
    data = pd.read_csv('D:/StrategyData' + '/' + 'strategy_data_' + str(file)[0:4] + str(file)[5:7] + '.csv', encoding = 'GBK').set_index('Unnamed: 0')
    datause = data[['idiovalotility', 'bp', 'operatingprofit', 'GreenblattROCTTM', 'alpha', 'earningnetincome', 'ROETTM', 'GrossProfitOnAssetTTM']].dropna()
    datause = (datause - datause.mean()) / datause.std()
    select1 = datause[(datause['alpha'] <= 0.401) & (datause['idiovalotility'] > 1.106) & (datause['beta'] <= 0.561)]
    select2 = datause[(datause['alpha'] > 0.401) & (datause['alpha'] <= 1.273) & (datause['beta'] <= -1.157) & (datause['GrossProfitMarginTTM'] <= -0.007) & (datause['cap'] <= -0.127)]
    select3 = datause[(datause['alpha'] > 0.401) & (datause['alpha'] <= 1.273) & (datause['beta'] <= -1.157) & (datause['GrossProfitMarginTTM'] > -0.007) & (datause['VolatilityAnnully'] <= 0.37)]
    select4 = datause[(datause['alpha'] > 0.401) & (datause['alpha'] <= 1.273) & (datause['beta'] > -1.157) & (datause['ebittevttm'] <= -0.197) & (datause['cap'] <= -0.199)]
    select5 = datause[(datause['alpha'] > 1.273) & (datause['ebittevttm'] > -0.231) & (datause['GrossProfitMarginTTM'] > -0.007) & (datause['VolatilityAnnully'] <= 0.844)]
    #droplist = datause[(datause['alpha'] > 0.369) & (datause['idiovalotility'] <= -1.064)]
    select = pd.concat([select1, select2, select3, select4, select5], axis = 0)
    #if len(droplist) != 0:
        #a = select.drop(index = droplist.index.tolist())
    num = len(select)
    reward = data.ix[select.index.tolist(),:][['return', 'MonthlyExcessReturn']]
    backtest.append([file[14:-4], reward.mean()[0], reward.mean()[1], num])
    print ('done')
    
backtest_table = pd.DataFrame(backtest)
backtest_table.columns = ['time', 'return', 'MonthlyExcessReturn', 'stocknum']
backtest_table = backtest_table.set_index('time')
BackTest = pd.DataFrame.cumprod(1+backtest_table)
BackTest['return'].plot()
BackTest['MonthlyExcessReturn'].plot()
